{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "couldn't import doomish\n",
      "Couldn't import doom\n",
      "\u001b[33mWARN: Environment '<class 'gym_ple.ple_env.PLEEnv'>' has deprecated methods '_step' and '_reset' rather than 'step' and 'reset'. Compatibility code invoked. Set _gym_disable_underscore_compat = True to disable this behavior.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "from dqn_agent import DQNAgent\n",
    "\n",
    "from gym.wrappers import Monitor\n",
    "\n",
    "import numpy as np\n",
    "import gym\n",
    "import gym_ple\n",
    "import utils\n",
    "from matplotlib import pyplot as plt\n",
    "from collections import deque\n",
    "\n",
    "def deque_append_list_elm(deque, list_data):\n",
    "    for data in list_data:\n",
    "        deque.append(data)\n",
    "        \n",
    "\n",
    "env = gym.make('FlappyBird-v0')\n",
    "env = Monitor(env, directory='./play_1e-3', force=True, video_callable=lambda ep: True, mode='evaluation')\n",
    "env.seed(0)\n",
    "state_num = 12\n",
    "action_sample = env.action_space.sample()\n",
    "action_num = env.action_space.n if isinstance(action_sample, int) else len(action_sample)\n",
    "\n",
    "agent = DQNAgent(state_num, action_num, buffer_size=1000000, batch_size=128, device=\"cpu\", hidden_unit=64)\n",
    "agent.load_model_params('FlappyBird-v0/DQN-lr_1_e-3', 380)\n",
    "\n",
    "idx = 0\n",
    "stack = deque(maxlen=12)\n",
    "for eps in range(10):\n",
    "    img = env.reset()\n",
    "    b_x, b_y, o_x1, o_y1, o_x2, o_y2 = utils.get_feature_points(img)\n",
    "    fea_dist = utils.get_feature_distances(img)/300\n",
    "    deque_append_list_elm(stack,fea_dist)\n",
    "    deque_append_list_elm(stack,fea_dist)\n",
    "    deque_append_list_elm(stack,fea_dist)\n",
    "    deque_append_list_elm(stack,fea_dist)\n",
    "    while True:\n",
    "        idx += 1\n",
    "        state = np.array(stack)\n",
    "        action = agent.choose_opt_action(state)\n",
    "        img, reward, done, _ = env.step(action)\n",
    "        \n",
    "        fea_dist = utils.get_feature_distances(img)/300\n",
    "        deque_append_list_elm(stack, fea_dist)\n",
    "        \n",
    "        if done:\n",
    "            break\n",
    "\n",
    "env.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
